package cn.kgc.convert;

import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.DataAccessException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @描述
 * @author：LY
 * @2018/11/8 10:19
 */
public class Convert implements Converter<String,Date> {

    private String [] strs = {"yyyy-MM-dd","yyyy/MM/dd","yyyy:MM:dd","yyyy年MM月dd日"};

    public String[] getStrs() {
        return strs;
    }

    public void setStrs(String[] strs) {
        this.strs = strs;
    }

    @Override
    public Date convert(String str) {
        SimpleDateFormat simpleDateFormat = null;
        for (String s:strs) {
            simpleDateFormat = new SimpleDateFormat(s);
            try {
                return simpleDateFormat.parse(str);
            } catch (ParseException e) {
                continue;
            }
        }
        throw new RuntimeException("没有可以匹配的：yyyy-MM-dd\",\"yyyy/MM/dd\",\"yyyy:MM:dd\",\"yyyy年MM月dd日");
    }



}
